package com.shawn.server.controller;

import com.shawn.autoconfig.template.OssTemplate;
import com.shawn.autoconfig.template.SmsTemplate;
import com.shawn.commons.utils.JwtUtils;
import com.shawn.dubbo.api.UserInfoApi;
import com.shawn.model.entity.BlackList;
import com.shawn.model.entity.Settings;
import com.shawn.model.entity.User;
import com.shawn.model.entity.UserInfo;
import com.shawn.model.vo.PageResult;
import com.shawn.model.vo.RelationUserInfoVo;
import com.shawn.model.vo.SettingsVo;
import com.shawn.model.vo.UserInfoVo;
import com.shawn.server.interceptor.ThreadContext;
import com.shawn.server.service.UserInfoService;
import com.shawn.server.service.UserService;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/users")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserService userService;


    @GetMapping("/{uid}/alreadyLove")
    public ResponseEntity whetherAlreadyLove(@PathVariable Long uid){
        Long userId = ThreadContext.getUserId();
        boolean isAlreadyLove = userService.whetherAlreadyLove(userId,uid);
           return ResponseEntity.ok(isAlreadyLove);
    }

    /**
     * 根据用户id获取关注用户,互相关注用户,及粉丝信息
     *
     * @param page     页面
     * @param pageSize 页面大小
     * @param nickname 昵称
     * @param type     类型
     * @return {@link ResponseEntity}
     */
    @GetMapping("/friends/{type}")
    public ResponseEntity getUserInfo(@RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(name = "pagesize",defaultValue = "10") Integer pageSize,
                                      String nickname,@PathVariable Integer type){
        PageResult<RelationUserInfoVo> pageResult = userInfoService.getUserInfo(page,pageSize,nickname,type);
        return ResponseEntity.ok(pageResult);
    }

    @GetMapping
    public ResponseEntity getUserInfo(String userID){
        Long id = ThreadContext.getUserId();
        if (userID!=null){
            id = Long.valueOf(userID);
        }
        //查询该用户信息
        UserInfoVo userInfoVo = userInfoService.findDetailById(id);

        return ResponseEntity.ok(userInfoVo);
    }

    /**
     * 更新用户信息
     *
     * @param token    令牌
     * @param userInfo 用户信息
     * @return {@link ResponseEntity}
     */

    @PutMapping
    public ResponseEntity updateUserInfo( @RequestBody  UserInfo userInfo ) throws ParseException {
        //判断token是否已过期
        Long id = ThreadContext.getUserId();
        userInfo.setId(id);
        userInfoService.updateUserInfo(userInfo);
        return ResponseEntity.ok("用户信息已更新");
    }

    @PostMapping("/header")
    public ResponseEntity updateHead( MultipartFile headPhoto) throws IOException {
        //判断token是否已过期
        Long id = ThreadContext.getUserId();

        //更新头像
        userInfoService.updateHeadPhoto(headPhoto, id);

        return ResponseEntity.ok("用户头像已更新!");
    }

    @GetMapping("/settings")
    public ResponseEntity getSettings(){
        //获取当前用户id
        Long userId = ThreadContext.getUserId();
        //根据id查询用户设置
        SettingsVo settingsVo = userInfoService.getSettings(userId);
        return ResponseEntity.ok(settingsVo);
    }

    /**
     * 设置问题
     *
     * @return {@link ResponseEntity}
     */
    @PostMapping("/questions")
    public ResponseEntity setQuestion(@RequestBody Map<String,String> map){
        //通过token获取用户id
        Long userId = ThreadContext.getUserId();
        userInfoService.updateQuestion(userId,map.get("content"));
        return ResponseEntity.ok("访问问题已设置!");
    }

    @PostMapping("/notifications/setting")
    public ResponseEntity updateSettings(@RequestBody Settings settings){
        //通过token获取用户id
        Long userId = ThreadContext.getUserId();
        settings.setUserId(userId);
        userInfoService.updateSettings(settings);
        return ResponseEntity.ok("通用设置已更新!");
    }

    /**
     * 发送验证码
     *
     * @return {@link ResponseEntity}
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity sendVerificationCode(){
        String userPhone = ThreadContext.getUserPhone();
        return userInfoService.sendVerificationCode(userPhone);
    }

    /**
     * 检查验证码
     *
     * @param map 地图
     * @return {@link ResponseEntity}
     */
    @PostMapping("/phone/checkVerificationCode")
    public ResponseEntity checkVerificationCode(@RequestBody Map<String,String> map){
        //获取手机号
        String userPhone = ThreadContext.getUserPhone();
        //获取验证码
        String inputCode = map.get("verificationCode");
       Boolean verification =  userInfoService.checkVerificationCode(userPhone,inputCode);
        Map<String, Boolean> hm = new HashMap<>();
        hm.put("verification",verification);
        return ResponseEntity.ok(hm);
    }
    @PostMapping("/phone")
    public ResponseEntity updatePhoneNumber(@RequestBody Map<String,String>map){
        String newPhoneNumber = map.get("phone");
        //获取用户id
        Long userId = ThreadContext.getUserId();
        User user = new User();
        user.setId(userId);
        user.setMobile(newPhoneNumber);
        userService.update(user);
        return ResponseEntity.ok("手机号码已修改!");
    }

    @GetMapping("/blacklist")
    public ResponseEntity getBlackList(@RequestParam(defaultValue = "1") Integer page,@RequestParam(name = "pagesize",defaultValue = "10") Integer pageSize){
        Long userId = ThreadContext.getUserId();
        PageResult<UserInfo> pageResult = userInfoService.getBlackList(page,pageSize,userId);
        return ResponseEntity.ok(pageResult);
    }

    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable("uid")Long userId ){
        userInfoService.removeBlackList(userId);
        return ResponseEntity.ok("黑名单已删除");
    }

    @GetMapping("/counts")
    public ResponseEntity getFriendsDetail(){
        Long userId = ThreadContext.getUserId();
        Map<String, Integer> hm = userInfoService.getFriendsDetail(userId);
        return ResponseEntity.ok(hm);
    }
}
